<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Controllers\AdminController;
use App\Models\Notice;
use Illuminate\Http\Request;

class NoticeController extends AdminController
{
	protected $title = 'Notice Manager';

	protected function title()
    {
        return $this->title;
    }

    

	protected function grid()
    {
    	$notice = new Notice();

        $grid = new Grid($notice);

        $grid->column('id', __('ID'))->sortable();
        $grid->column('content', __('Content'))->display(function ($content)  {
        	$str = [];
        	if ($content) {
        		foreach($content as $k=>$v) {
        			if (empty($v)) continue;
	        		$str[] = ($k+1).': '.$v['title'];
	        	}
	        }
        	return '<pre>'.implode('<br>', $str).'</pre>';
		});
        $grid->column('delay_type', __('Type'))->display(function ($delay_type) use ($notice) {
        	return $notice->delay_type_arr[$delay_type];
        });
        $grid->column('delay_limit', __('Remain times'));
        $grid->column('status', __('Status'))->display(function ($status) {
        	if (1==$status) {
        		return "<span class='label label-success'>On</span>";
        	} else {
        		return "<span class='label label-danger'>Off</span>";
        	}
        });
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    protected function detail($id)
    {
        $notice = new Notice();

        $show = new Show($notice::findOrFail($id));

        $show->field('id', 'ID');
        $show->field('content')->unescape()->as(function ($content) {
		    $str = '';
        	if ($content) {
        		foreach($content as $k=>$v) {
        			if (empty($v)) continue;
	        		$str .= '<pre>'.$v['title'].': '.$v['value'].'</pre>';
	        	}
	        }
	        return $str;
		});
		$show->field('delay_type')->as(function ($delay_type) use ($notice) {
			return $notice->delay_type_arr[$delay_type];
		});
        $show->field('delay_hour_minute');
        $show->field('delay_hour_minute_end');
        $show->field('delay_day');
        $show->field('is_delay_limit')->as(function ($is_delay_limit ) {
        	return 1==$is_delay_limit ? '无次数限制' : '有次数限制';
        });
        $show->field('delay_limit');
        $show->field('notice_type')->as(function ($notice_type) use ($notice) {
        	return $notice->notice_type_arr[$notice_type];
        });
        $show->field('status')->as(function ($status) {
        	return 1==$status ? '开启' : '关闭';
        });
        $show->field('remote_ip');
        $show->field('created_at', trans('admin.created_at'));
        $show->field('updated_at', trans('admin.updated_at'));

        return $show;
    }

    public function form()
    {
        $notice = new Notice();

        $form = new Form($notice);

        $form->display('id', 'ID');
        $form->table('content', function ($table) {
		    $table->text('title');
		    $table->text('value');
		});

        $form->select('delay_type', '定时类型')->options($notice->delay_type_arr)->when(1, function ($form) {
        	$form->time('delay_hour_minute','执行时间')->format('HH:mm');
        	$form->time('delay_hour_minute_end','执行时间（结束）')->format('HH:mm');
        	$form->text('delay_day','每几小时执行一次');
        })->when(2, function ($form) {
        	$form->time('delay_hour_minute','执行时间')->format('HH:mm');
        	$form->text('delay_day','几号执行')->attribute(['placeholder'=>'几号执行，eg:20-22-25，0代表每天执行']);
        })->when(3, function ($form) {
        	$form->time('delay_hour_minute','执行时间')->format('HH:mm');
        	$form->text('delay_day','每周几执行')->attribute(['placeholder'=>'每周几执行，eg:1-3-5-7']);
        })
        // ->when(4, function ($form) {
        // 	$form->time('delay_hour_minute','执行时间')->format('HH:mm');
        // 	$form->text('delay_day','每月几号执行')->attribute(['placeholder'=>'每月几号执行，eg:8-15-26']);
        // })
        ->required();
        
        $states = [
		    'on'  => ['value' => 1, 'text' => 'enable', 'color' => 'success'],
		    'off' => ['value' => 0, 'text' => 'disable', 'color' => 'danger'],
		];
        $form->switch('is_delay_limit', '无次数限制')->states($states);
        $form->text('delay_limit', '执行次数')->default(0);
        $form->select('notice_type', '提醒等级')->options($notice->notice_type_arr);
        $form->switch('status','开启')->states($states);
        $form->hidden('remote_ip')->value($_SERVER['REMOTE_ADDR']);

        return $form;
    }
}